#include "stdafx.h"
#include "PlayerUtility.h"
#include "tlhelp32.h"
#include <string>

CPlayerUtility::CPlayerUtility()
{
}


CPlayerUtility::~CPlayerUtility()
{
}

void KillAppByExeName(const wchar_t* name, bool killall /*= false*/)
{
	if (nullptr == name) {
		return;
	}

	CStringW strName = name;
	strName = strName.MakeUpper();

	PROCESSENTRY32 pe32;
	pe32.dwSize = sizeof(pe32);

	HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (hProcessSnap == INVALID_HANDLE_VALUE)
	{
		OutputDebugString(L"\nCreateToolhelp32Snapshot failed");
		return;
	}
	
	bool flag = ::Process32First(hProcessSnap, &pe32);
	while (flag) {
		CStringW exefile = pe32.szExeFile;
		int npos = exefile.Find(L"\\");
		if (npos != -1) {
			exefile = exefile.Right(exefile.GetLength() - npos - 1);
		}
		exefile = exefile.MakeUpper();

		if (exefile == strName) {
			TerminateProcess(OpenProcess(PROCESS_TERMINATE, true,
				pe32.th32ProcessID), 0);

			if (false == killall) {
				break;
			}
		}

		flag = ::Process32Next(hProcessSnap, &pe32);
	}
	
	::CloseHandle(hProcessSnap);
}

bool CheckMainExeExist(const wchar_t* name)
{
	if (nullptr == name) {
		return false;
	}

	if (wcslen(name) == 0) {
		return true;
	}

	CStringW strName = name;
	strName = strName.MakeUpper();

	PROCESSENTRY32 pe32;
	pe32.dwSize = sizeof(pe32);

	HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (hProcessSnap == INVALID_HANDLE_VALUE)
	{
		OutputDebugString(L"\nCreateToolhelp32Snapshot failed");
		return false;
	}

	bool flag = ::Process32First(hProcessSnap, &pe32);
	while (flag) {
		CStringW exefile = pe32.szExeFile;
		int npos = exefile.Find(L"\\");
		if (npos != -1) {
			exefile = exefile.Right(exefile.GetLength() - npos - 1);
		}
		exefile = exefile.MakeUpper();

		if (exefile == strName) {
			return true;
		}

		flag = ::Process32Next(hProcessSnap, &pe32);
	}

	::CloseHandle(hProcessSnap);

	return false;
}

void SendCopyDataMsgByHWND(HANDLE wnd, const wchar_t* data)
{
	OutputDebugString(L"\nSendCopyDataMsgByHWND");
	OutputDebugString(data);

	COPYDATASTRUCT cs;
	cs.dwData = 1;
	cs.cbData = wcslen(data) * 2 + 2;
	cs.lpData = (void*)data;
	::SendMessage((HWND)wnd, WM_COPYDATA, 0, (LPARAM)&cs);
}

#define FILE_SETTING L"\\upupoo\\settings.ini"
#define SECTION_PLAYER L"Root"
#define KEY_VOLUMN L"DefaultVolume"
#define KEY_MUTE L"IsGlobalMute"
#define KEY_SPEED L"DefaultPlaySpeed"
#define KEY_REPEATTYPE L"PlayRepeatType"
#define KEY_PLAYMODE L"PlayMode"

std::wstring GetCfgPath() {
	static std::wstring ret;
	if (false == ret.empty()) {
		return ret;
	}

	TCHAR szPath[MAX_PATH] = { 0 };
	SHGetSpecialFolderPath(NULL, szPath, CSIDL_PERSONAL, FALSE);
	ret = szPath;
	ret += FILE_SETTING;
	return ret;
}

int g_volumn = 0;
int g_mute = 0;
float g_speed = 100.0f;
int g_repeattype = 0;
int g_playmode = 0;
void ReadCfg()
{
	WCHAR buf[MAX_PATH] = { 0 };
	g_volumn = GetPrivateProfileInt(SECTION_PLAYER, KEY_VOLUMN, 0, GetCfgPath().c_str());
	g_mute = GetPrivateProfileInt(SECTION_PLAYER, KEY_MUTE, 0, GetCfgPath().c_str());
	g_speed = (float)GetPrivateProfileInt(SECTION_PLAYER, KEY_SPEED, 100, GetCfgPath().c_str());
	g_repeattype = GetPrivateProfileInt(SECTION_PLAYER, KEY_REPEATTYPE, 0, GetCfgPath().c_str());
	g_playmode = GetPrivateProfileInt(SECTION_PLAYER, KEY_PLAYMODE, 0, GetCfgPath().c_str());
}

int GetCfgVolumn()
{
	return g_volumn;
}

bool GetCfgMute()
{
	return g_mute == 0;
}

float GetCfgPlaySpeed()
{
	return g_speed;
}

int GetCfgPlayRepeatType()
{
	return g_repeattype;
}

int GetCfgPlayMode()
{
	return g_playmode;
}

float StretchFitRect(RECT& src, RECT dest)
{
	float ratio = 1.0f;
	int srcwidth = src.right - src.left;
	int srcheight = src.bottom - src.top;
	int destwidth = dest.right - dest.left;
	int destheight = dest.bottom - dest.top;
	if (destwidth == 0 || destheight == 0) {
		return ratio;
	}

	float widthradio = srcwidth / (float)destwidth;
	float heightradio = srcheight / (float)destheight;
	if (widthradio < heightradio) {
		ratio = widthradio;
	}
	else {
		ratio = heightradio;
	}

	src = { 0,0, (long)round(widthradio / ratio), (long)round(srcheight / ratio) };
	return ratio;
}

bool eq(const wchar_t* left, const wchar_t* right)
{
	CStringW strLeft = left;
	CStringW strRight = right;
	strLeft.Trim();
	strRight.Trim();
	strLeft = strLeft.MakeLower();
	strRight = strRight.MakeLower();

	return strLeft == strRight;
}
